package net.jalbum.filters;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.awt.image.ColorConvertOp;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
import javax.swing.ImageIcon;

import net.jalbum.filterManager.FilterProperties.FilterCategory;
import se.datadosen.jalbum.Msg;

public class SharpenFilterPlugin extends BasicFilter {

    private static final long serialVersionUID = 1720559385062856569L;

    public SharpenFilterPlugin() {
        // TODO: Localize
//		this.name = "Sharpen Filter";
        this.name = Msg.getString(this, "filters.sharpen.name");

//            this.shortName = "Sharpen";
        this.shortName = Msg.getString(this, "filters.sharpen.shortName");

        this.icon = new ImageIcon(this.getClass().getResource("res/sharpen.png"));
        this.author = "David Fichtmueller";
        this.version = "1.0";
        this.description = Msg.getString(this, "filters.sharpen.description");

        //		this.description = "Make the image appear a little sharper.";
        this.category = FilterCategory.OTHER;
        this.prescale = false;
        this.postscale = true;
    }

    @Override
    public BufferedImage renderImage(BufferedImage bi) {
        // the following code is by David Ekholm from the SharpenFilter

        // Prevent bug http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6585922
        if (bi.getType() == BufferedImage.TYPE_3BYTE_BGR) {
            bi = convertType(bi, BufferedImage.TYPE_INT_RGB);
        }

        final Kernel kernel = new Kernel(3, 3, new float[]{0.f, -0.3f, 0.f, -0.3f, 2.2f, -0.3f, 0.f, -0.3f, 0.f});

        final BufferedImageOp op = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
        bi = op.filter(bi, null);
        return bi;
    }
}
